package cn.hx.mall.permissions.service.impl;

import cn.hx.mall.permissions.dao.AdminDao;
import cn.hx.mall.permissions.entity.Admin;
import cn.hx.mall.permissions.entity.page.Page;
import cn.hx.mall.permissions.service.AdminService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 后台用户表(Admin)表服务实现类
 *
 * @author makejava
 * @since 2022-11-02 17:02:34
 */
@Service("adminService")
public class AdminServiceImpl implements AdminService {
    @Resource
    private AdminDao adminDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Admin queryById(Long id) {
        return this.adminDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Admin> queryAllByLimit(int offset, int limit) {
        return this.adminDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param admin 实例对象
     * @return 实例对象
     */
    @Override
    public Admin insert(Admin admin) {
        this.adminDao.insert(admin);
        return admin;
    }

    /**
     * 修改数据
     *
     * @param admin 实例对象
     * @return 实例对象
     */
    @Override
    public Admin update(Admin admin) {
        this.adminDao.update(admin);
        return this.queryById(admin.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.adminDao.deleteById(id) > 0;
    }

    @Override
    public List<Admin> findAll(Admin admin) {
        return adminDao.findAll(admin);
    }

    @Override
    public List<Admin> query(String str) {
        return  adminDao.query(str);
    }

    @Override
    public Page getPageInfo(Page page , Admin admin) {
      int count =  adminDao.count(admin);
        Integer limit = page.getLimit();
        int tPage = count%limit==0? count/limit:count/limit+1;
        page.setCntpage(tPage);
        page.setCount(count);
        return page;
    }

    @Override
    public void deleteResourcesByAdminId(Integer adminId) {
        adminDao.deleteResourcesByAdminId(adminId);
    }

    @Override
    public void allocRoles(Integer adminId, List<String> ids1) {
        for (String s : ids1) {
            adminDao.allocRoles(adminId,Integer.valueOf(s));
        }
    }
}